package leetcode.median;

/**
 * ClassName: $670_MaximumSwap
 * Description:
 * date: 2022-02-17 10:10
 *
 * @author liyifan
 */
public class $670_MaximumSwap {
    public int maximumSwap(int num) {
        char[] chars = String.valueOf(num).toCharArray();
        int len = chars.length;
        int maxIdx = len - 1;
        int[] maxArr = new int[len];
        for (int i = len - 1;i >= 0;i--) {
            if (chars[i] > chars[maxIdx]) {
                maxIdx = i;
            }
            maxArr[i] = maxIdx;
        }

        for (int i = 0;i < len;i++) {
            int maxIndex = maxArr[i];
            if(chars[i] != chars[maxIndex]){
                char temp = chars[i];
                chars[i] = chars[maxIndex];
                chars[maxIndex] = temp;
                break;
            }
        }
        return Integer.parseInt(new String(chars));

    }
}
